/*
 * Google 1-grams aggregieren, um nach häufigkeit sortierte liste von wörtern zu kriegen
 */
package complet.app.tool.experiment;

import complet.helper.IOHelper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.TreeMap;

public class experiment3_1 {

    private static BufferedReader in = IOHelper.openReadFile("E:\\paul\\Dokumente\\MyDocuments\\NetBeans Workspace\\Completion\\inputfiles\\ngrams\\1-grams.txt");
    private static TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
    private static TreeMap<String, Integer> tm_s = null;
    private static BufferedWriter out1 = IOHelper.openWriteFile("E:\\paul\\Dokumente\\MyDocuments\\NetBeans Workspace\\Completion\\stats\\225_250T\\experiment3\\words.dat");

    private static final Comparator<String> COMP = new Comparator<String>() {

        public int compare(String s1, String s2) {
            if (tm.get(s1) > tm.get(s2)) {
                return -1;
            }
            return 1;
        }
    };

    public static void main(String[] args) {
        try {
            String strLine;
            String line[];
            while (true && (strLine = in.readLine()) != null) {
                line = strLine.split("\t");
                if(line[0].length() > 1){
                    tm.put(line[0], Integer.parseInt(line[1]));
                }
            }
        } catch (IOException ex) {
            IOHelper.logError("(experiment3) Failed to read input file: " + ex.getMessage());
        }

        tm_s = new TreeMap<String, Integer>(COMP);
        tm_s.putAll(tm);

        try {

            for(String key: tm_s.keySet()){
                out1.write(key + "\t " + tm.get(key) + "\n");
            }
            out1.flush();
            out1.close();
        } catch (IOException ex) {
            IOHelper.logError("(experiment1) Failed to write result file: " + ex.getMessage());
        }
    }
}
